{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "0509.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/0509.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Uw3S8RV8mkOx",
        "colab_type": "text"
      },
      "source": [
        "## 2.2 数据操作"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jaHunee7xxdB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install mxnet "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_POyYZEpx05E",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "4b4cef02-a192-4ca2-bca9-b26feef854ad"
      },
      "source": [
        "from mxnet import nd \n",
        "\n",
        "x = nd.arange(12)\n",
        "x"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.]\n",
              "<NDArray 12 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "L_oD-GLSyFAz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "b6a1a0f4-288b-43c0-aa3d-c04ef0b98a22"
      },
      "source": [
        "x.shape "
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(12,)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dGE2iGboyKuE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c10132e3-bd36-4d0e-927a-3e0a1adfc1cb"
      },
      "source": [
        "x.size "
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "12"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mcSDqPqdyNG2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "215b158b-e1c5-43c1-d04b-46006c5233e4"
      },
      "source": [
        "X = x.reshape((3, 4))\n",
        "X"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KQGU8tt5yUC0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 168
        },
        "outputId": "7486cd1d-8dbe-4735-8254-b9a54a27fd95"
      },
      "source": [
        "nd.zeros((2, 3, 4))"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[[0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]]\n",
              "\n",
              " [[0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]]]\n",
              "<NDArray 2x3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bOB4r6ADyebi",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "0e04e610-92d0-4fb3-a097-72193913d248"
      },
      "source": [
        "nd.ones((3, 4))"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[1. 1. 1. 1.]\n",
              " [1. 1. 1. 1.]\n",
              " [1. 1. 1. 1.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CTSJirXbyhoC",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "d20fbfee-cd1b-495d-cabf-18fbf07b51d7"
      },
      "source": [
        "Y = nd.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])\n",
        "Y"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[2. 1. 4. 3.]\n",
              " [1. 2. 3. 4.]\n",
              " [4. 3. 2. 1.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qk5EOAgOyvP6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "0caeb0ea-885b-426f-fb3a-9d20bde67f45"
      },
      "source": [
        "nd.random.normal(0, 1, shape=(3, 4))"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 2.2122064   0.7740038   1.0434403   1.1839255 ]\n",
              " [ 1.8917114  -1.2347414  -1.771029   -0.45138445]\n",
              " [ 0.57938355 -1.856082   -1.9768796  -0.20801921]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QRCLarSny5Dz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "7e409d96-b959-4f44-d893-5fdf00d5da44"
      },
      "source": [
        "X + Y"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 2.  2.  6.  6.]\n",
              " [ 5.  7.  9. 11.]\n",
              " [12. 12. 12. 12.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hB98CXZvy7MS",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "3753a041-4b43-4d1e-bd7c-de4613a6f53a"
      },
      "source": [
        "X * Y"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  8.  9.]\n",
              " [ 4. 10. 18. 28.]\n",
              " [32. 27. 20. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Pbo4jlKLy9kZ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "877a7ce7-8afc-4f8b-e61b-096a8ebdf5f0"
      },
      "source": [
        "X / Y"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.    1.    0.5   1.  ]\n",
              " [ 4.    2.5   2.    1.75]\n",
              " [ 2.    3.    5.   11.  ]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vnDq4kJ4zE6i",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "49810d2b-b365-4a28-b2d5-04356fb56bc0"
      },
      "source": [
        "Y.exp()"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 7.389056   2.7182817 54.59815   20.085537 ]\n",
              " [ 2.7182817  7.389056  20.085537  54.59815  ]\n",
              " [54.59815   20.085537   7.389056   2.7182817]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PRr3JK2wzHZB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "71ab84a3-15db-43cd-d5c9-a604a79e9964"
      },
      "source": [
        "nd.dot(X, Y.T)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 18.  20.  10.]\n",
              " [ 58.  60.  50.]\n",
              " [ 98. 100.  90.]]\n",
              "<NDArray 3x3 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qmkmRTP6zPlQ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 151
        },
        "outputId": "0d4d42c6-be51-4e12-f31e-86a09d7b2e45"
      },
      "source": [
        "nd.concat(X, Y, dim=0)"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]\n",
              " [ 2.  1.  4.  3.]\n",
              " [ 1.  2.  3.  4.]\n",
              " [ 4.  3.  2.  1.]]\n",
              "<NDArray 6x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XJYZXIbOziNh",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "bcf05f99-fe27-4b12-8f54-ffb6d046f0cf"
      },
      "source": [
        "nd.concat(X, Y, dim=1)"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.  2.  1.  4.  3.]\n",
              " [ 4.  5.  6.  7.  1.  2.  3.  4.]\n",
              " [ 8.  9. 10. 11.  4.  3.  2.  1.]]\n",
              "<NDArray 3x8 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WhC4d16UzlXI",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "d4688cf9-6e7d-4dbc-e4ac-8df69bdbe825"
      },
      "source": [
        "X == Y"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0. 1. 0. 1.]\n",
              " [0. 0. 0. 0.]\n",
              " [0. 0. 0. 0.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TEAPT6jUzp0B",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "e02ae98d-fae8-40c8-a7d9-2b6c65501436"
      },
      "source": [
        "X.sum()"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[66.]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S4Ob2SBQzyRI",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 134
        },
        "outputId": "2770ca60-0c91-4b69-adab-c9942d1cd768"
      },
      "source": [
        "A = nd.arange(3).reshape((3, 1))\n",
        "B = nd.arange(2).reshape((1, 2))\n",
        "A, B"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(\n",
              " [[0.]\n",
              "  [1.]\n",
              "  [2.]]\n",
              " <NDArray 3x1 @cpu(0)>, \n",
              " [[0. 1.]]\n",
              " <NDArray 1x2 @cpu(0)>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CSuhyK0S0FbW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "397fe31c-1115-420d-e11d-44825803198a"
      },
      "source": [
        "A + B "
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0. 1.]\n",
              " [1. 2.]\n",
              " [2. 3.]]\n",
              "<NDArray 3x2 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-Jf-ZWnP0Hnu",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "27980914-45f8-4c4f-b95b-bd7bad860fe4"
      },
      "source": [
        "X[1:3]"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 2x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "B3RKMTjq0QA2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "2cde8c8b-d675-4bc6-8fc8-91a674a93012"
      },
      "source": [
        "X[1, 2] = 9 \n",
        "X"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  9.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2EWqqtFN0Th2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "3e5cc100-2f41-4e89-d644-0d0f78784880"
      },
      "source": [
        "X[1:2, :] = 12 \n",
        "X"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [12. 12. 12. 12.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AQgbBoLP0gkF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "ec14036c-bf78-4684-f015-30449ec6d1e6"
      },
      "source": [
        "before = id(Y)\n",
        "Y = Y + X \n",
        "id(Y) == before"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YukpSSrH0wA-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c6665ccc-9734-4c76-d4d1-ccd52b86d580"
      },
      "source": [
        "Z = Y.zeros_like()\n",
        "before = id(Z)\n",
        "Z[:] = X + Y \n",
        "id(Z) == before "
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nqkLS_iO1BaF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "b34d0805-d20c-473c-a9b3-5dd883293fdd"
      },
      "source": [
        "nd.elemwise_add(X, Y, out=Z)\n",
        "id(Z) == before "
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S5uHXFaW1Oxb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "60ded764-911a-49ed-dea8-358b879ab59a"
      },
      "source": [
        "before = id(X)\n",
        "X += Y \n",
        "id(X) == before "
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m3GB7HsR1T9r",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "3e259f4c-b9c1-4385-8d02-bdc03c4dfe20"
      },
      "source": [
        "import numpy as np  \n",
        "P = np.ones((2, 3))\n",
        "D = nd.array(P)\n",
        "D "
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[1. 1. 1.]\n",
              " [1. 1. 1.]]\n",
              "<NDArray 2x3 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "k0yU3Equ1vBs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "913347ff-478c-46ee-c6a9-ed27a5337617"
      },
      "source": [
        "D.asnumpy()"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1., 1.],\n",
              "       [1., 1., 1.]], dtype=float32)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-vDEy76V75Bp",
        "colab_type": "text"
      },
      "source": [
        "## 2.3 自动求梯度"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gdrXy6cQ14uk",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "d8a8bec2-24ae-47e5-929b-d0220bb75530"
      },
      "source": [
        "from mxnet import autograd, nd \n",
        "\n",
        "x = nd.arange(4).reshape((4, 1))\n",
        "x "
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0.]\n",
              " [1.]\n",
              " [2.]\n",
              " [3.]]\n",
              "<NDArray 4x1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0UKcjNro2EK4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x.attach_grad()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WTeFf5VN2KzI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "with autograd.record():\n",
        "    y = 2 * nd.dot(x.T, x)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EVNxgC2Z2SRR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y.backward()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DMZpiz4g2mT5",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "85c879ef-39df-40e0-e471-1e879da715af"
      },
      "source": [
        "assert (x.grad - 4 * x).norm().asscalar() == 0 \n",
        "x.grad"
      ],
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.]\n",
              " [ 4.]\n",
              " [ 8.]\n",
              " [12.]]\n",
              "<NDArray 4x1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YSaUKmaW2zKw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "abd493f2-ce79-4096-c2f4-838af572e936"
      },
      "source": [
        "print(autograd.is_training())\n",
        "with autograd.record():\n",
        "    print(autograd.is_training())"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "False\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QCUWHOHT3Zyn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def f(a):\n",
        "    b = a * 2 \n",
        "    while b.norm().asscalar() < 1000:\n",
        "        b = b * 2 \n",
        "    if b.sum().asscalar() > 0:\n",
        "        c = b \n",
        "    else:\n",
        "        c = 100 * b \n",
        "    return c "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NqKiibFe4F8l",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "11698f55-f8bf-4ef2-da99-dd9dbc2a5735"
      },
      "source": [
        "a = nd.random.normal(shape=1)\n",
        "a"
      ],
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[-0.77569664]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nRssLO5H4PUL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "a.attach_grad()\n",
        "with autograd.record():\n",
        "    c = f(a)\n",
        "c.backward()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Cr-PI-ed5pg8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "94f1cf4b-f028-4d63-a45b-3cc0ede8c918"
      },
      "source": [
        "a.grad == c / a "
      ],
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[1.]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3DvAd67R5-Yp",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "5f39c506-4b14-4b74-f18d-95ba45152b6b"
      },
      "source": [
        "a.grad "
      ],
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[204800.]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yWwJYmjS6LDn",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "26f6d68d-b840-4bbe-bcca-17367dd91498"
      },
      "source": [
        "a "
      ],
      "execution_count": 50,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[-0.77569664]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QiTSawGI6NTg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "bbd39e59-6778-491f-9ffd-e50cc21ab1ee"
      },
      "source": [
        "c "
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[-158862.67]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0sUv_dhN6PFg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        },
        "outputId": "901508e6-c25a-4b0d-e929-412bb9e773eb"
      },
      "source": [
        "from mxnet.gluon import nn \n",
        "\n",
        "print(dir(nn))"
      ],
      "execution_count": 52,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "['Activation', 'AvgPool1D', 'AvgPool2D', 'AvgPool3D', 'BatchNorm', 'Block', 'Conv1D', 'Conv1DTranspose', 'Conv2D', 'Conv2DTranspose', 'Conv3D', 'Conv3DTranspose', 'Dense', 'Dropout', 'ELU', 'Embedding', 'Flatten', 'GELU', 'GlobalAvgPool1D', 'GlobalAvgPool2D', 'GlobalAvgPool3D', 'GlobalMaxPool1D', 'GlobalMaxPool2D', 'GlobalMaxPool3D', 'HybridBlock', 'HybridLambda', 'HybridSequential', 'InstanceNorm', 'Lambda', 'LayerNorm', 'LeakyReLU', 'MaxPool1D', 'MaxPool2D', 'MaxPool3D', 'PReLU', 'ReflectionPad2D', 'SELU', 'Sequential', 'Swish', 'SymbolBlock', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'activations', 'basic_layers', 'conv_layers']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pyA3TuT26yBd",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 504
        },
        "outputId": "98a53ddd-ebc3-43e3-c231-0e4a3b385327"
      },
      "source": [
        "help(nd.ones_like)"
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Help on function ones_like:\n",
            "\n",
            "ones_like(data=None, out=None, name=None, **kwargs)\n",
            "    Return an array of ones with the same shape and type\n",
            "    as the input array.\n",
            "    \n",
            "    Examples::\n",
            "    \n",
            "      x = [[ 0.,  0.,  0.],\n",
            "           [ 0.,  0.,  0.]]\n",
            "    \n",
            "      ones_like(x) = [[ 1.,  1.,  1.],\n",
            "                      [ 1.,  1.,  1.]]\n",
            "    \n",
            "    \n",
            "    \n",
            "    Parameters\n",
            "    ----------\n",
            "    data : NDArray\n",
            "        The input\n",
            "    \n",
            "    out : NDArray, optional\n",
            "        The output NDArray to hold the result.\n",
            "    \n",
            "    Returns\n",
            "    -------\n",
            "    out : NDArray or list of NDArrays\n",
            "        The output of this function.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W8V4NB5W68mW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "nd.ones_like??"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}